package pl.shenlon.poker.gui;

import java.util.Observable;

import pl.shenlon.poker.cards.Card;
import pl.shenlon.poker.cards.Hand;

public class DefaultHandComponentModel extends HandComponentModel {

	private Hand hand;

	public DefaultHandComponentModel() {
	}

	@Override
	public Card getCard(int index) {
		return hand.getCard(index);
	}

	@Override
	public int getCardsCount() {
		return hand.getCardsCount();
	}

	@Override
	public Hand getHand() {
		return hand;
	}

	@Override
	public int getVisibleCardsCount() {
		return hand.getVisibleCardsCount();
	}

	@Override
	public void setCard(int index, Card card) {
		this.hand.setCard(index, card);
		setChanged();
		notifyObservers(String.format("setCard(%d, %s)", index, card));
	}

	@Override
	public void setHand(Hand hand) {
		if (this.hand != null) {
			this.hand.deleteObserver(this);
		}
		this.hand = hand;
		this.hand.addObserver(this);
		setChanged();
		notifyObservers(String.format("setHand(%s)", hand));
	}

	@Override
	public void showCard(Card card) {
		card.setFaceUp(true);
		setChanged();
		notifyObservers(String.format("showCard(%s)", card));
	}

	@Override
	public void showCards() {
		hand.show();
		setChanged();
		notifyObservers("showCards()");
	}

	@Override
	public void showCards(int... indexes) {
		hand.show(indexes);
		setChanged();
		notifyObservers(String.format("showCards(%s)",
				java.util.Arrays.toString(indexes)));
	}

	@Override
	public void update(Observable obj, Object arg) {
		setChanged();
		notifyObservers("update()");

	}
}